home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / spheremap / HACKS / cview2smap.c next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  7.4 KB  |  337 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1998.  */
  3.  
  4. /* This program is freely distributable without licensing fees
  5.    and is provided without guarantee or warrantee expressed or
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. /* cview2smap.c - warp 6 cube views into sphere map */
  9.  
  10. #include <math.h>
  11. #include <stdio.h>
  12. #include <GL/glut.h>
  13.  
  14. #include "smapmesh.h"
  15.  
  16. #if defined(GL_EXT_texture_object) && !defined(GL_VERSION_1_1)
  17. #define glBindTexture(A,B)     glBindTextureEXT(A,B)
  18. #endif
  19. #if defined(GL_EXT_copy_texture) && !defined(GL_VERSION_1_1)
  20. #define glCopyTexImage2D(A, B, C, D, E, F, G, H)    glCopyTexImage2DEXT(A, B, C, D, E, F, G, H)
  21. #endif
  22.  
  23. GLfloat up[3] = { 0, 1, 0 };
  24. GLfloat eye[3] = { 0, 0, -20 };
  25. GLfloat obj[3] = { 0, 0, 0 };
  26. int outline = 0;
  27. int bufswap = 1;
  28. int texdim = 128;
  29.  
  30. static int W, H;
  31.  
  32. int angle = 0;
  33. int downx, downy;
  34.  
  35. void
  36. reshape(int w, int h)
  37. {
  38.     W = w;
  39.     H = h;
  40. }
  41.  
  42. void
  43. drawView(int drawCenterObject)
  44. {
  45.     /* right green small cube (+5,0,-8) */
  46.     glPushMatrix();
  47.       glTranslatef(5.0, 0.0, -8.0);
  48.       glRotatef(-45, 1.0, 0.0, 1.0);
  49.       glColor3f(0.0, 1.0, 0.0);
  50.       glutSolidCube(2.0);
  51.     glPopMatrix();
  52.     /* left red cube (-5,0,-8) */
  53.     glPushMatrix();
  54.       glTranslatef(-5.0, 0.0, -8.0);
  55.       glRotatef(45, 1.0, 0.0, 1.0);
  56.       glColor3f(1.0, 0.0, 0.0);
  57.       glutSolidCube(6.0);
  58.     glPopMatrix();
  59.     /* left blue cube (-7,0,0); */
  60.     glPushMatrix();
  61.       glTranslatef(-7.0, 0.0, 0.0);
  62.       glColor3f(0.0, 0.0, 1.0);
  63.       glutSolidCube(5.0);
  64.     glPopMatrix();
  65.     /* right cyan big cube (+7,0,0) */
  66.     glPushMatrix();
  67.       glTranslatef(7.0, 0.0, 0.0);
  68.       glRotatef(30, 1.0, 1.0, 0.0);
  69.       glColor3f(0.0, 1.0, 1.0);
  70.       glutSolidCube(5.0);
  71.     glPopMatrix();
  72.     /* distant yellow sphere (0,0,+10) */
  73.     glPushMatrix();
  74.       glTranslatef(0.0, 0.0, 10.0);
  75.       glColor3f(1.0, 1.0, 0.0);
  76.       glutSolidSphere(7.0, 8, 8);
  77.     glPopMatrix();
  78.     /* top white sphere (0,+5,0) */
  79.     glPushMatrix();
  80.       glTranslatef(0.0, 5.0, 0.0);
  81.       glColor3f(1.0, 1.0, 1.0);
  82.       glutSolidSphere(2.0, 8, 8);
  83.     glPopMatrix();
  84.     /* bottom magenta big sphere (0,-6,0) */
  85.     glPushMatrix();
  86.       glTranslatef(0.0, -6.0, 0.0);
  87.       glColor3f(1.0, 0.0, 1.0);
  88.       glutSolidSphere(4.0, 8, 8);
  89.     glPopMatrix();
  90.  
  91.     if (drawCenterObject) {
  92.         glPushMatrix();
  93.             glScalef(3.0, 3.0, 3.0);
  94.             glColor3f(1.0, 1.0, 1.0);
  95.             glutSolidIcosahedron();
  96.         glPopMatrix();
  97.     }
  98. }
  99.  
  100. void
  101. setAreaAndClear(GLint x, GLint y, GLsizei w, GLsizei h)
  102. {
  103.     glViewport(x, y, w, h);
  104.     glScissor(x, y, w, h);
  105.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  106. }
  107.  
  108. /* You need to reposition the lights everytime view */
  109. /* transformation changes. */
  110. void
  111. positionLights(void)
  112. {
  113.     static GLfloat light1Pos[4] = { -41.0, 41.0, -41.0, 1.0 };
  114.     static GLfloat light2Pos[4] = { +41.0, 0.0, -41.0, 1.0 };
  115.     static GLfloat light3Pos[4] = { -41.0, 0.0, +41.0, 1.0 };
  116.     static GLfloat light4Pos[4] = { +41.0, 0.0, +41.0, 1.0 };
  117.  
  118.     glLightfv(GL_LIGHT0, GL_POSITION, light1Pos);
  119.     glLightfv(GL_LIGHT1, GL_POSITION, light2Pos);
  120.     glLightfv(GL_LIGHT2, GL_POSITION, light3Pos);
  121.     glLightfv(GL_LIGHT3, GL_POSITION, light4Pos);
  122. }
  123.  
  124. void
  125. snagImageAsTexture(GLuint texobj)
  126. {
  127.     glBindTexture(GL_TEXTURE_2D, texobj);
  128.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
  129.         GL_LINEAR);
  130.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  131.         GL_LINEAR);
  132.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  133.     /* Clamp to avoid artifacts from wrap around in texture. */
  134.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  135.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  136.  
  137.     glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0,
  138.         texdim, texdim, 0);
  139. }
  140.  
  141. struct {
  142.     GLfloat angle;
  143.     GLfloat x, y, z;
  144. } faceInfo[6] = {
  145.     {   0.0, +1.0,  0.0,  0.0 },  /* front */
  146.     {  90.0, -1.0,  0.0,  0.0 },  /* top */
  147.     {  90.0, +1.0,  0.0,  0.0 },  /* bottom */
  148.     {  90.0,  0.0, -1.0,  0.0 },  /* left */
  149.     {  90.0,  0.0, +1.0,  0.0 },  /* right */
  150.     { 180.0, -1.0,  0.0,  0.0 }   /* back */
  151. };
  152.  
  153. void
  154. configFace(int i)
  155. {
  156.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  157.     glMatrixMode(GL_MODELVIEW);
  158.     glLoadIdentity();
  159.     glRotatef(faceInfo[i].angle,
  160.         faceInfo[i].x, faceInfo[i].y, faceInfo[i].z);
  161.     gluLookAt(obj[0], obj[1], obj[2],  /* "eye" at object */
  162.               eye[0], eye[1], eye[2],  /* looking at eye */
  163.               up[0], up[1], up[2]);
  164.     positionLights();
  165. }
  166.  
  167. void
  168. display(void)
  169. {
  170.     static GLuint texobjs[6] = { 1, 2 ,3, 4, 5, 6 };
  171.     int i;
  172.  
  173.     glMatrixMode(GL_PROJECTION);
  174.     glLoadIdentity();
  175.     gluPerspective(90.0, 1.0, 0.5, 40.0);
  176.     glViewport(0, 0, texdim, texdim);
  177.     glScissor(0, 0, texdim, texdim);
  178.     glEnable(GL_SCISSOR_TEST);
  179.     glEnable(GL_DEPTH_TEST);
  180.  
  181.     /* front view (center) */
  182.     for (i=0; i<6; i++) {
  183.         configFace(i);
  184.         drawView(0);
  185.         snagImageAsTexture(1+i);
  186.     }
  187.  
  188.     glDisable(GL_SCISSOR_TEST);
  189.     glClear(GL_COLOR_BUFFER_BIT);
  190.     glViewport(0, 0, W, H);
  191.     glMatrixMode(GL_PROJECTION);
  192.     glLoadIdentity();
  193.     gluOrtho2D(0, 1, 0, 1);
  194.     glMatrixMode(GL_MODELVIEW);
  195.     glLoadIdentity();
  196.     glDisable(GL_DEPTH_TEST);
  197.     glEnable(GL_TEXTURE_2D);
  198.     drawSphereMapMesh(texobjs);
  199.     glDisable(GL_TEXTURE_2D);
  200.     if (outline) {
  201.         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  202.         glDisable(GL_LIGHTING);
  203.         glColor3f(1.0, 1.0, 1.0);
  204.         drawSphereMapMesh(texobjs);
  205.         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  206.         glEnable(GL_LIGHTING);
  207.     }
  208.  
  209.     glDisable(GL_SCISSOR_TEST);
  210.  
  211.     /* initial clear */
  212.     glViewport(0, 0, W, H);
  213.  
  214.     if (bufswap) {
  215.         glutSwapBuffers();
  216.     }
  217. }
  218.  
  219. void
  220. menu(int value)
  221. {
  222.     switch (value) {
  223.     case 1:
  224.         glEnable(GL_LIGHTING);
  225.         printf("enable\n");
  226.         break;
  227.     case 2:
  228.         glDisable(GL_LIGHTING);
  229.         printf("disable\n");
  230.         break;
  231.     case 3:
  232.         outline = 1;
  233.         break;
  234.     case 4:
  235.         outline = 0;
  236.         break;
  237.     case 5:
  238.         glDrawBuffer(GL_FRONT);
  239.         glReadBuffer(GL_FRONT);
  240.         bufswap = 0;
  241.         break;
  242.     case 6:
  243.         glDrawBuffer(GL_BACK);
  244.         glReadBuffer(GL_BACK);
  245.         bufswap = 1;
  246.         break;
  247.     case 7:
  248.         texdim = 64;
  249.         break;
  250.     case 8:
  251.         texdim = 128;
  252.         break;
  253.     case 9:
  254.         texdim = 256;
  255.         break;
  256.     }
  257.     glutPostRedisplay();
  258. }
  259.  
  260. void
  261. motion(int x, int y)
  262. {
  263.     angle += (x - downx);
  264.     angle = angle % 360;
  265.     eye[0] = sin(angle * 3.14159/180.0) * -20;
  266.     eye[2] = cos(angle * 3.14159/180.0) * -20;
  267.     eye[1] = eye[1] + 0.1 * (y - downy);
  268.     if (eye[1] > +25) eye[1] = +25;
  269.     if (eye[1] < -25) eye[1] = -25;
  270.     glutPostRedisplay();
  271.     downx = x;
  272.     downy = y;
  273. }
  274.  
  275. void
  276. mouse(int button, int state, int x, int y)
  277. {
  278.     if (button == GLUT_LEFT_BUTTON) {
  279.         if (state == GLUT_DOWN) {
  280.             downx = x;
  281.             downy = y;
  282.         }
  283.     }
  284. }
  285.  
  286. void
  287. initGraphicsState(void)
  288. {
  289.     GLfloat lightColor[4] = { 0.6, 0.6, 0.6, 1.0 };  /* white */
  290.  
  291.     glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
  292.     glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor);
  293.     glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor);
  294.     glLightfv(GL_LIGHT2, GL_DIFFUSE, lightColor);
  295.     glLightfv(GL_LIGHT3, GL_DIFFUSE, lightColor);
  296.     glEnable(GL_LIGHTING);
  297.     glEnable(GL_LIGHT0);
  298.     glEnable(GL_LIGHT1);
  299.     glEnable(GL_LIGHT2);
  300.     glEnable(GL_LIGHT3);
  301.     glEnable(GL_DEPTH_TEST);
  302.     glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
  303.     glEnable(GL_COLOR_MATERIAL);
  304.     glEnable(GL_NORMALIZE);
  305. }
  306.  
  307. int
  308. main(int argc, char **argv)
  309. {
  310.     makeSphereMapMesh();
  311.  
  312.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  313.     glutCreateWindow("cview2smap");
  314.     glutDisplayFunc(display);
  315.     glutReshapeFunc(reshape);
  316.  
  317.     initGraphicsState();
  318.  
  319.     glutCreateMenu(menu);
  320.     glutAddMenuEntry("light on", 1);
  321.     glutAddMenuEntry("light off", 2);
  322.     glutAddMenuEntry("outline", 3);
  323.     glutAddMenuEntry("no outline", 4);
  324.     glutAddMenuEntry("single buffer", 5);
  325.     glutAddMenuEntry("double buffer", 6);
  326.     glutAddMenuEntry("face tex dim = 64", 7);
  327.     glutAddMenuEntry("face tex dim = 128", 8);
  328.     glutAddMenuEntry("face tex dim = 256", 9);
  329.     glutAttachMenu(GLUT_RIGHT_BUTTON);
  330.  
  331.     glutMouseFunc(mouse);
  332.     glutMotionFunc(motion);
  333.  
  334.     glutMainLoop();
  335.     return 0;
  336. }
  337.